export default function () {
    var exampledrag = document.querySelector("#exampledrag");

    function handleImage(imageFile) {
        var reader = new FileReader();
        reader.onload = function (event) {
            $('#editor3').wysiwyg('shell').insertHTML(`<img src="${event.target.result}">`);
        }
        reader.readAsDataURL(imageFile);
    }

    exampledrag.addEventListener("dragover", function( event ) {
        // prevent default to allow drop
        event.preventDefault();
    }, false);

    document.addEventListener("drop", function( event ) {

        var data = event.dataTransfer.items;
        for (var i = 0; i < data.length; i += 1) {
            if ((data[i].kind == 'string') && (data[i].type.match('^text/plain'))) {
                // This item is the target node
                data[i].getAsString(function (s){
                    console.log('------------------------------------');
                    console.log(s);
                    console.log('------------------------------------');
                });
            } else if ((data[i].kind == 'string') &&
                (data[i].type.match('^text/html'))) {
                // Drag data item is HTML
                console.log("... Drop: HTML");

                data[i].getAsString(function (s){
                    console.log('------------------------------------');
                    console.log(s);
                    console.log('------------------------------------');

                    var regex = /src="[\w:\/.\-\_]+/g
                    var srcarr = s.match(regex);
                    var src = "";

                    if (srcarr && srcarr.length > 0) {
                        src = srcarr[0].replace(/src="/g, '');
                        $('#editor3').wysiwyg('shell').insertHTML(`<img src="${src}">`);
                    }

                });
            } else if ((data[i].kind == 'string') &&
                (data[i].type.match('^text/uri-list'))) {
                // Drag data item is URI
                console.log("... Drop: URI");

                data[i].getAsString(function (s){
                    console.log('------------------------------------');
                    console.log(s);
                    console.log('------------------------------------');
                });
            } else if ((data[i].kind == 'file') &&
                (data[i].type.match('^image/'))) {
                // Drag data item is an image file
                var f = data[i].getAsFile();
                console.log("... Drop: File ");

                handleImage(f);
            }
        }


        // prevent default action (open as link for some elements)
        event.preventDefault();
    }, false);
}